#include <iostream>
#include <fstream>
#include <math.h>
#include <algorithm>
#include <string.h>

using namespace std;

int luni[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

class data{
public:
	int zz, ll, aa;
	bool operator < (const data &other) const {
		return aa < other.aa || 
		aa == other.aa && ll < other.ll ||
		aa == other.aa && ll == other.ll && zz < other.zz;
	}

	friend ostream & operator << (ostream &out, data &a);
};

ostream & operator << (ostream &out, data &a){
	out << a.zz << " " << a.ll << " " << a.aa;
	return out;
}

class calendar{
	int n;
	data d;
public:
	void citire(){
		cin >> n;
		cin >> d.zz >> d.ll >> d.aa;
	}

	int diferenta(data a, data b){
		if(b < a)
			swap(a, b);
		if(a.aa == b.aa){
			if(a.ll == b.ll)
				return b.zz - a.zz;
			int dif = 0;
			dif = luni[a.ll] - a.zz;
			dif += b.zz;
			for(int i = a.ll + 1; i < b.ll; i ++)
				dif += luni[i];

			return dif;
		}
		int sum = 0;
		for(int i = 1; i <= 12; i ++)
			sum += luni[i];

		int dif = luni[a.ll] - a.zz;
		for(int i = a.ll + 1; i <= 12; i ++)
			dif += luni[i];
		dif += b.zz;
		for(int i = 1; i < b.ll; i ++)
			dif += luni[i];

		dif += max(b.aa - a.aa - 1, 0) * sum;

		return dif;
	}
	//Cam brute :-""
	data dupa(){
		data dd = d;
		for(int i = 1; i <= n; i ++){
			dd.zz ++;
			if(dd.zz > luni[dd.ll])
				dd.zz = 1, dd.ll ++;
			if(dd.ll > 12)
				dd.ll = 1, dd.aa ++;
		}

		return dd;
	}
	data inainte(){
		data dd = d;
		for(int i = 1; i <= n; i ++){
			dd.zz --;
			if(dd.zz < 1){
				if(dd.ll == 1)
					dd.ll = 12, dd.aa --;
				else
					dd.ll --;
				dd.zz = luni[dd.ll];
			}
		}

		return dd;
	}
};

int main(){
	calendar c;
	c.citire();
	data dupa = c.dupa();
	data inainte = c.inainte();
	
	cout << dupa << "\n";
	cout << inainte << "\n";

	return 0;
}